<template>
  <div v-loading="isloading" class="prossececontainer">
    <div class="selectHeader">
      <div class="select-wrapper">
        <div class="selectBox">
          <div class="selectTitle">
            <i class="iconfont icon-sousuo" />按条件查询
          </div>

          <div>
            <el-input
              v-model="listquery.key"
              clearable
              placeholder="编制单位名称"
              style="width: 250px"
            ></el-input>

            <el-select
              placeholder="编制单位资质"
              v-model="listquery.qualification"
              clearable
            >
              <el-option
                v-for="(item, index) in unittype"
                :key="index"
                :label="item"
                :value="item"
              />
            </el-select>
            <el-button type="success" icon="el-icon-search" @click="search"
              >搜索</el-button
            >
            <el-button type="warning" icon="el-icon-refresh-left" @click="reset"
              >重置</el-button
            >
          </div>
        </div>
      </div>
    </div>

    <div>
      <div class="header">
        <div class="header-title">
          <i class="el-icon-d-arrow-right"></i>编制单位列表
        </div>

        <div class="uploadbutton">
          <!-- <el-button size="small" type="primary" @click="addunits">
            <i class="el-icon-plus"></i>导入</el-button
          > -->
            <el-button
              type="warning"
              @click="handleUpdate(currentrow)"
              size="mini"
              :disabled="currentrow==null"
              plain
              ><i class="el-icon-edit"></i>编辑</el-button
            >
            <el-button
              type="danger"
              @click="handleDelete(currentrow.id)"
              :disabled="currentrow==null"
              size="mini"
              plain
              ><i class="el-icon-delete"></i>删除</el-button
            >
          <el-button size="small" type="primary" @click="open">
            <i class="el-icon-plus"></i>增加</el-button
          >
        </div>
      </div>
      <el-table
        :data="tableData"
        @row-click="handleRowClick"  
        @selection-change="handleSelectionChange"
        ref="multipleTable"
        stripe
        border
        style="width: 100%"
        class="eltable"
      >
        <el-table-column type="selection" width="55"> </el-table-column>
        <el-table-column label="序号" width="100" type="index" />
        <el-table-column prop="name" label="编制单位名称" />
        <el-table-column prop="qualification" label="编制资质 " width="250">
        </el-table-column>
        <el-table-column prop="auditBatch" label="认证审核批次 " width="200" />
        <el-table-column prop="userNames" label="用户名 " width="350">
          <template scope="scope">
            <span v-if="scope.row.userNames">{{ scope.row.userNames }}</span>
            <span v-else>暂未注册</span>
          </template>
        </el-table-column>
      
      </el-table>
      <el-pagination
        v-if="tableData.length > 0"
        class="elpagi"
        :total="total"
        @size-change="getdata"
        @current-change="getdata"
        @prev-click="getdata"
        @next-click="getdata"
        :current-page.sync="listquery.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size.sync="listquery.pageSize"
        layout="total,sizes, prev, pager, next, jumper"
      />
    </div>
    <el-dialog
      :title="dialogStatus == 'add' ? ' 编制单位新增' : '编制单位信息修改'"
      :visible.sync="isDialogShow"
      width="30%"
      :append-to-body="true"
      class="dialog1"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="150px"
        label-position="left"
      >
        <el-form-item label="编制单位名称:" prop="name">
          <el-input
            type="text"
            :rows="8"
            placeholder="请输入内容"
            size="mini"
            clearable
            v-model="form.name"
            style="width: 100%"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="编制资质:" prop="qualification">
          <el-select
            v-model="form.qualification"
            placeholder="请选择"
            size="mini"
          >
            <el-option
              v-for="item in unittype"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="认证审核批次:" prop="auditBatch">
          <el-input
            type="text"
            :rows="8"
            placeholder="请输入批次"
            size="mini"
            clearable
            v-model="form.auditBatch"
            style="width: 100%"
          >
          </el-input>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isDialogShow = false">取 消</el-button>
        <el-button
          type="primary"
          @click="dialogStatus === 'add' ? submitForm('form') : updateData()"
          >提交</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { profilelist } from "@/api/index.js";
import { handleCofirm } from "@/utils/confirm";
import { getqualificationlist, edit, add, del } from "@/api/shenhe.js";
import queryMixin from "@/mixins/countquery.js";
import multiMixin from "@/mixins/multi.js";
import { export_json_to_excel } from "@/utils/export2Excel.js";

export default {
  mixins: [queryMixin,multiMixin],
  data() {
    return {
      listquery: {
        pageSize: 10,
        currentPage: 1,
        key: "",
        qualification: "",
        village: "",
      },
      formv: {},
      filterShow: true,
      //新增编辑弹窗
      isDialogShow: false,
      dialogStatus: "",
      rules: {
        name: [{ required: true, message: "请选择领取区域", trigger: "blur" }],
        qualification: [
          { required: true, message: "请输入领取人名称", trigger: "blur" },
        ],
        auditBatch: [
          { required: true, message: "请选择领取时间", trigger: "blur" },
        ],
      },
      form: {
        name: "",
        qualification: "",
        auditBatch: "",
      },
      iswraploading: false,
      allvilages: [],
      unittype: [],
      isloading: false,
      changeDialogVisible: false,
      cityChildren: [],
      tableData: [],
    };
  },
  mounted() {
    this.getdata();
    this.getunittype();
  },
  methods: {
    search() {
      this.listquery.currentPage = 1
         this.listquery.pageSize=10
      this.getdata();
    },
    reset() {
      this.listquery = {
        pageSize: 10,
        currentPage: 1,
        key: "",
        qualification: "",
        village: "",
      };
    },

    open() {
      this.form = {
        name: "",
        qualification: "",
        auditBatch: "",
      };
      this.dialogStatus = "add";
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
      this.isDialogShow = true;
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const temp = Object.assign({}, this.form);
          add(temp)
            .then((v) => {
              this.getdata();
              this.$notify({
                title: "成功",
                message: "添加成功",
                type: "success",
              });
              this.isDialogShow = false;
            })
            .catch((error) => {
              this.$notify.error({
                title: "错误",
                message: error,
              });
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    handleUpdate(row) {
      this.form = Object.assign({}, row); // copy obj
      this.dialogStatus = "edit";
      this.isDialogShow = true;
      this.$nextTick(() => {
        this.$refs["form"].clearValidate();
      });
    },
    updateData() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          edit(this.form).then(() => {
            this.getdata();
            this.isDialogShow = false;
            this.$notify({
              title: "成功",
              message: "修改成功",
              type: "success",
              duration: 2000,
            });
          });
        }
      });
    },
    handleDelete(id) {
      handleCofirm("确认要删除吗？", "warning").then((res) => {
        del(id)
          .then((v) => {
            this.getdata();
            this.$notify({
              title: "成功",
              message: "删除成功",
              type: "success",
            });
            this.isDialogShow = false;
          })
          .catch((error) => {
            this.$notify.error({
              title: "错误",
              message: error,
            });
          });
      });
    },

    download() {
      this.iswraploading = true;
      let query = {
        pageSize: 9999,
        currentPage: 1,
        nickName: this.listquery.nickName,
        qualification: this.listquery.qualification,
        village: this.listquery.village,
      };
      profilelist(query).then((res) => {
        let tablelist = res.data.content;
        this.downloadExcel(tablelist);
        this.iswraploading = false;
      });
    },
    downloadExcel(tabledatalist) {
      let header = Object.keys(tabledatalist[0]);
      let list2 = [];
      for (var i = 0; i < tabledatalist.length; i++) {
        let list1 = [];
        for (var j = 0; j < header.length; j++) {
          let obj = tabledatalist[i];
          list1.push(obj[header[j]]);
        }
        list2.push(list1);
      }
      export_json_to_excel({
        header: header,
        data: list2,
        filename: "编制单位统计信息",
      });
    },
    openmore(villages) {
      this.changeDialogVisible = true;
      this.allvilages = villages;
    },

    async getdata() {
      this.isloading = true;
      let res = await profilelist(this.listquery);
      this.tableData = res.data.content;
      this.total = res.data.totalElements;
      this.isloading = false;
    },
    async getunittype() {
      let res = await getqualificationlist();
      this.unittype = res.data.filter(function (s) {
        return s && s.trim();
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "../../../../public/css/table.scss";
@import "../../../../public/css/countheader.scss";
.spandiv {
  margin-right: 5px;
}
::v-deep .el-select .el-input {
  width: 265px;
}
.prossececontainer {
  margin: 5px;
  padding-bottom: 10px;
  overflow-y: auto;
  position: relative;
  height: 100%;
}
ul {
  margin: 0;
  padding: 5px;
  list-style: none;
  border: 1px dashed #ccc;
  max-height: 250px;
  overflow-y: auto;
  li {
    line-height: 25px;
  }
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  line-height: 45px;
  width: 100%;
  .header-title {
    font-size: 15px;
    font-weight: 600;
    color: #4778c7;
  }
}
.allvilagestitle {
  line-height: 35px;
  color: #4778c7;
}

.more {
  color: #4cc8c7;
  margin-left: 10px;
  &:hover {
    color: orange;
  }
}
</style>
